<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/7
 * Time: 20:22
 */

namespace app\controller;


use app\model\app\AppSms;
use app\model\app\AppSmsContent;

class Sms
{
    public function send(){
        header('content-type:text/html;charset=utf8');
        $find = AppSms::where('status',1)->find();
        $appid = $find->app_id;
        $key = $find->api_key;
        $secret = $find->api_pwd;
        $content = AppSmsContent::where('id',$find->template_id)->find();
        $mobile = input('mobile');
        $apiKey = $key;
        $apiSecret = $secret;
        $appId = $appid;
        $url = "https://api.onbuka.com/v3/sendSms";
        $timeStamp = time();
        $sign = md5($apiKey.$apiSecret.$timeStamp);
        $dataArr['appId'] = $appId;
        $dataArr['numbers'] = $mobile;
        $dataArr['content'] = $content->content;
        $dataArr['senderId'] = '';
        $data = json_encode($dataArr);
        $headers = array('Content-Type:application/json;charset=UTF-8',"Sign:$sign","Timestamp:$timeStamp","Api-Key:$apiKey");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return json(['code' => 200,'msg' => '','data' => []]);
    }

    public function code(){
        $code = rand(100000,999999);
        $mobile = input('mobile');
        cache($mobile,$code,600);
        return json(['code' => 200,'msg' => '','data' => $code]);
    }

    public function v_code(){
        $code = input('code');
        $mobile = input('mobile');
        $v_code = cache($mobile);
        if ($code == $v_code)
            return json('OK');
        return json('NO');
    }
}